using System.Web;
using System.Web.Caching;

namespace FishSimulation {
    /// <summary>
    /// 
    /// </summary>
    public static class Cache {
        #region Members
        /// <summary>
        /// 
        /// </summary>
        private static HttpRuntime _Http;
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the <see cref="System.Object"/> with the specified key.
        /// </summary>
        /// <value></value>
        public static System.Web.Caching.Cache Values {
            get { return HttpRuntime.Cache; }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes the <see cref="Cache"/> class.
        /// </summary>
        static Cache() {            
            if (null == _Http) {
                try {
                    System.Threading.Monitor.Enter(typeof(Cache));
                    if (null == _Http) {
                        // Create an Http Content to give us access to the cache.
                        _Http = new HttpRuntime();
                    }
                }
                finally {
                    System.Threading.Monitor.Exit(typeof(Cache));
                }
            }            
        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds the specified key/value pair to the cache.
        /// The cached value will have no absolute expiration, 
        /// no sliding expiration, and the priority will be normal.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public static void Add(string key, object value) {
            Cache.Values.Add(key, value, null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                System.Web.Caching.Cache.NoSlidingExpiration,
                CacheItemPriority.Normal, null);
        }

        /// <summary>
        /// Clears cached values
        /// </summary>
        public static void Clear() {
            var e = Cache.Values.GetEnumerator();
            while (e.MoveNext()) {
                Cache.Values.Remove(e.Key.ToString());
            }
        }

        /// <summary>
        /// Returns an object from the Cache.Values collection.
        /// This allows call from a project that doesn't contain a
        /// reference to System.Web
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Get(string key) {
            return Cache.Values[key];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            Cache.Values.Remove(key);
        }

        /// <summary>
        /// Adds the key/value pair to the cache if the key
        /// doesn't already exist. If the given key already
        /// exists it updates the value to the new value given.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void Set(string key, object value) {
            if (Cache.Values[key] == null) {
                Add(key, value);
            }
            else {
                if (value != null)
                    Cache.Values[key] = value;
                else
                    Cache.Values.Remove(key);
            }
        }
        #endregion
    }
}